package manager_service.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manager_mapper.mapper.CourseTypeMapper;
import manager_service.service.BackCourseTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import test_common.constant.Constant;
import test_common.entity.CourseType;
import test_common.entity.CourseTypeExample;

import java.util.List;

/**
 * @author zhangzheng
 * @create 2021/2/1
 */
@Service
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
public class BackCourseTypeServiceImpl implements BackCourseTypeService {

    @Autowired
    private CourseTypeMapper courseTypeMapper;
    @Override
    public void addCourseType(CourseType courseType) {
        courseTypeMapper.insertSelective(courseType);
    }

//    @Override
//    public PageInfo<CourseType> showCourses(Integer pageNo,Integer pageSize) {
//        PageHelper.startPage(pageNo,pageSize);
//        List<CourseType> list=courseTypeMapper.selectByExample(new CourseTypeExample());
//        PageInfo<CourseType> typePageInfo=new PageInfo<>(list);
//        return typePageInfo;
//    }

    @Override
    public PageInfo<CourseType> showCourses(Integer pageNo,Integer pageSize,Integer parentId) {
        PageHelper.startPage(pageNo,pageSize);
        CourseTypeExample example=new CourseTypeExample();
        example.or()
                .andParentIdEqualTo(parentId);
        List<CourseType> list=courseTypeMapper.selectByExample(example);
        PageInfo<CourseType> typePageInfo=new PageInfo<>(list);
        return typePageInfo;
    }

    @Override
    public void modifyStatus(String courseTypeId) {
        CourseType courseType=courseTypeMapper.selectByPrimaryKey(Integer.parseInt(courseTypeId));
        if (courseType.getStatus()== Constant.COURSETYPE_DISABLE){
            courseType.setStatus(Constant.COURSETYPE_ENABLE);
            courseTypeMapper.updateByPrimaryKey(courseType);
        }
        else if (courseType.getStatus()==Constant.COURSETYPE_ENABLE){
            courseType.setStatus(Constant.COURSETYPE_DISABLE);
            courseTypeMapper.updateByPrimaryKey(courseType);
        }
    }
}
